<template>
  <div class="singer" v-loading="!singers.length">
    <IndexList :data="singers" class="list" @selectSinger="getSelectSinger"></IndexList>
    <router-view v-slot="{ Component }">
      <transition name="slide">
        <component :is="Component" :singer='selectedSinger'/>
      </transition>
    </router-view>
  </div>
</template>

<script>
import { getSingerList } from '@/service/singer'
import IndexList from '@/components/index-list/index-list.vue'
import storage from 'good-storage'
import { SINGER_KEY } from '@/js/constant'
export default {
  name: 'Singer',
  data () {
    return {
      singers: [],
      selectedSinger: null
    }
  },
  components: {
    IndexList
  },
  async created () {
    const result = await getSingerList()
    this.singers = result.singers
  },
  methods: {
    getSelectSinger (singer) {
      this.selectedSinger = singer
      this.cacheSinger(singer)
      this.$router.push({ path: `/singer/${singer.mid}` })
    },
    // 本地缓存singer对象，防止跳到二级路由刷新丢失，用到good-storage,封装了JSON.Stringfy和JSON.parse
    cacheSinger (singer) {
      storage.set(SINGER_KEY, singer)
    }
  }
}
</script>
<style lang="scss" scoped>
  .singer {
      position: fixed;
      width: 100%;
      top: 3.7527rem;
      bottom: 0;
    }
</style>
